package com.ymatou.iapi.livebuyer.testcase;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.UnSubscribeActivityBean;
import com.ymatou.iapi.livebuyer.service.UnSubscribeActivityCall;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveActivity;
import com.ymttest.database.model.LiveActivityProduct;
import com.ymttest.database.model.LiveActivitySubscribe;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*取消直播秒杀活动订阅
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UnSubscribeActivity{
	private static UnSubscribeActivityBean unsubscribeactivityBean;
	private static UnSubscribeActivityCall unsubscribeactivityCall;

	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static int sellerId2 = Integer.parseInt(EnvSetup.getData("autotest_seller2"));
	LiveRoomWapper LiveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("取消直播秒杀活动订阅");
	}

	@Before
	public void caseUp() {
		unsubscribeactivityBean=new UnSubscribeActivityBean();
		unsubscribeactivityCall=new UnSubscribeActivityCall();
		LiveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(unsubscribeactivityCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnSubscribeActivity_001() {
		Logger.start(true,"取消直播秒杀活动订阅查询成功");
		try {
			int uid=2;
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			Integer apid = LiveRoomCallService.Subscribeactivity(uid, createlive.getId(), liveacctivityid,productId);
			Thread.sleep(3000);
//			List<LiveActivityProduct> liveproducts = LiveRoomWapper.selectLiveactivityproduct(livea.getLiveActivityId(), productId);
			LiveActivitySubscribe DbLiveActivitySubscribe = LiveRoomWapper.selectLiveActivitySubscribeByExample(apid);
			Logger.verifyEquals(1, DbLiveActivitySubscribe.getState(), "验证status订阅");
			
			unsubscribeactivityBean.setUserId(uid);
			unsubscribeactivityBean.setLiveActivityProductId(apid);
			unsubscribeactivityBean.setLiveId(createlive.getId());
			unsubscribeactivityCall.setData(unsubscribeactivityBean);
			unsubscribeactivityCall.callService();			
			Logger.verifyEquals("200", unsubscribeactivityCall.getString("code"), "验证code");
			DbLiveActivitySubscribe = LiveRoomWapper.selectLiveActivitySubscribeByExample(apid);
			Logger.verifyEquals(0, DbLiveActivitySubscribe.getState(), "验证status订阅");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_UnSubscribeActivity_002() {
		Logger.start(true,"不输入liveid失败");
		try {
			int uid=2;
			int liveacctivityid=1;
			int liveid=1;
			unsubscribeactivityBean.setUserId(uid);
			unsubscribeactivityBean.setLiveActivityProductId(liveacctivityid);
//			unsubscribeactivityBean.setLiveId(liveid);
			unsubscribeactivityCall.setData(unsubscribeactivityBean);
			unsubscribeactivityCall.callService();			
			Logger.verifyEquals("100", unsubscribeactivityCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_UnSubscribeActivity_003() {
		Logger.start(true,"不输入liveActivityId失败");
		try {
			int uid=2;
			int liveacctivityid=1;
			int liveid=1;
			unsubscribeactivityBean.setUserId(uid);
//			unsubscribeactivityBean.setLiveActivityId(liveacctivityid);
			unsubscribeactivityBean.setLiveId(liveid);
			unsubscribeactivityCall.setData(unsubscribeactivityBean);
			unsubscribeactivityCall.callService();			
			Logger.verifyEquals("100", unsubscribeactivityCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_UnSubscribeActivity_004() {
		Logger.start(true,"不输入userid失败");
		try {
			int uid=2;
			int liveacctivityid=1;
			int liveid=1;
//			unsubscribeactivityBean.setUserId(uid);
			unsubscribeactivityBean.setLiveActivityProductId(liveacctivityid);
			unsubscribeactivityBean.setLiveId(liveid);
			unsubscribeactivityCall.setData(unsubscribeactivityBean);
			unsubscribeactivityCall.callService();			
			Logger.verifyEquals("100", unsubscribeactivityCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}